package com.rokyinfo.pdm.controller.v1;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rokyinfo.pdm.controller.BasicPath;
import com.rokyinfo.pdm.entity.DRadioInfo;
import com.rokyinfo.pdm.service.IDRadioInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * RADIO信息表 前端控制器
 * </p>
 *
 * @author YuanZhiJian
 * @since 2017-01-21
 */
@Api(value = "/radioInfo", tags = "RadioInfo", description = "通讯模块扩展信息")
@Controller
@RequestMapping(BasicPath.V1 + "/radioInfo")
public class DRadioInfoController {
    @Autowired
    private IDRadioInfoService mIDRadioInfoService;

    @ApiOperation(value = "获取通讯模块扩展信息列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Page<DRadioInfo>> getDRadioInfoList(@RequestParam(value = "offset", required = false, defaultValue = "1") Integer offset, @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit) throws Exception {
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        EntityWrapper<DRadioInfo> wrapper = new EntityWrapper<DRadioInfo>();
        return ResponseEntity.ok(mIDRadioInfoService.selectPage(new Page<DRadioInfo>(offset, limit), wrapper));
    }

    @ApiOperation(value = "新建通讯模块扩展信息", notes = "根据DRadioInfo对象创建通讯模块扩展信息")
    @ApiImplicitParam(name = "DRadioInfo", value = "通讯模块扩展信息实体DRadioInfo", required = true, dataType = "DRadioInfo")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseEntity postDRadioInfo(@RequestBody DRadioInfo DRadioInfo) {
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        if (mIDRadioInfoService.insert(DRadioInfo)) {
            return new ResponseEntity(HttpStatus.CREATED);
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "获取通讯模块扩展信息信息", notes = "根据url的SN来获取通讯模块扩展信息信息")
    @ApiImplicitParam(name = "sn", value = "设备SN", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public ResponseEntity<DRadioInfo> getDRadioInfo(@PathVariable String sn) {
        EntityWrapper<DRadioInfo> wrapper = new EntityWrapper<DRadioInfo>();
        DRadioInfo _mDRadioInfo = new DRadioInfo();
        _mDRadioInfo.setSn(sn);
        wrapper.setEntity(_mDRadioInfo);
        DRadioInfo mDRadioInfo = mIDRadioInfoService.selectOne(wrapper);
        if (mDRadioInfo != null) {
            return ResponseEntity.ok(mDRadioInfo);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @ApiOperation(value = "更新通讯模块扩展信息信息", notes = "根据url的SN来指定更新对象，并根据传过来的DRadioInfo信息来更新通讯模块扩展信息信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "设备SN", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "DRadioInfo", value = "通讯模块扩展信息实体DRadioInfo", required = true, dataType = "DRadioInfo")
    })
    @RequestMapping(value = "/{sn}", method = RequestMethod.PUT)
    public ResponseEntity putDRadioInfo(@PathVariable String sn, @RequestBody DRadioInfo DRadioInfo) {
        EntityWrapper<DRadioInfo> wrapper = new EntityWrapper<DRadioInfo>();
        DRadioInfo.setSn(sn);
        wrapper.setEntity(DRadioInfo);
        if (mIDRadioInfoService.update(DRadioInfo, wrapper)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "删除通讯模块扩展信息", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "sn", value = "设备SN", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{sn}", method = RequestMethod.DELETE)
    public ResponseEntity deleteDRadioInfo(@PathVariable String sn) {
        EntityWrapper<DRadioInfo> wrapper = new EntityWrapper<DRadioInfo>();
        DRadioInfo mDRadioInfo = new DRadioInfo();
        mDRadioInfo.setSn(sn);
        wrapper.setEntity(mDRadioInfo);
        if (mIDRadioInfoService.delete(wrapper)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
